package com.authine.cloudpivot.ext.vo.zhende.order;

import com.authine.cloudpivot.ext.vo.zhende.PushOrderGoodsModel;
import com.authine.cloudpivot.ext.vo.zhende.PushOrderModel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;

import javax.xml.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
        "header",
        "body"
})
@XmlRootElement(name = "soapenv:Envelope")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class EnvelopeOrder {

    @XmlAttribute(name = "xmlns:soapenv")
    private String soapenv = "http://schemas.xmlsoap.org/soap/envelope/";
    @XmlAttribute(name = "xmlns:s4")
    private String eapp = "http://ZDSAP.COM/S4";

    @XmlElement(name = "soapenv:Header")
    protected com.authine.cloudpivot.ext.vo.zhende.order.Header header;
    @XmlElement(name = "soapenv:Body")
    protected com.authine.cloudpivot.ext.vo.zhende.order.Body body;

    public EnvelopeOrder(com.authine.cloudpivot.ext.vo.zhende.order.Header header, com.authine.cloudpivot.ext.vo.zhende.order.Body body) {
        this.header = header;
        this.body = body;
    }

    public static EnvelopeOrder toXml(PushOrderModel pushOrderModel) {
        List<PushOrderGoodsModel> pushOrderGoodsModelList = pushOrderModel.getPushOrderGoodsModelList();
        List<com.authine.cloudpivot.ext.vo.zhende.order.MT1240MMPOCREATEREQ.TITEM> titemList = new ArrayList<>();

        for (PushOrderGoodsModel detail : pushOrderGoodsModelList) {
            com.authine.cloudpivot.ext.vo.zhende.order.MT1240MMPOCREATEREQ.TITEM item = new com.authine.cloudpivot.ext.vo.zhende.order.MT1240MMPOCREATEREQ.TITEM();
                BeanUtils.copyProperties(detail, item);
                titemList.add(item);
        }
        EnvelopeOrder envelopeOrder = new EnvelopeOrder(new com.authine.cloudpivot.ext.vo.zhende.order.Header(), com.authine.cloudpivot.ext.vo.zhende.order.Body.builder().mt1240MMPOCREATEREQ(
                com.authine.cloudpivot.ext.vo.zhende.order.MT1240MMPOCREATEREQ.builder()
                        .ispoheader(com.authine.cloudpivot.ext.vo.zhende.order.MT1240MMPOCREATEREQ.ISPOHEADER.builder()
                                .bsart(pushOrderModel.getBsart())
                                .bukrs(pushOrderModel.getBukrs())
                                .ekgrp(pushOrderModel.getEkgrp())
                                .ekorg(pushOrderModel.getEkorg())
                                .lifnr(pushOrderModel.getLifnr())
                                .submi(pushOrderModel.getSubmi())
                                .zfornum(pushOrderModel.getZfornum())
                                .build())
                        .titem(titemList)
                        .build())
                .build());
        return envelopeOrder;
    }
}
